package com.oocl.chatroom.client.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Date;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.plaf.basic.BasicButtonUI;

import com.oocl.chatroom.client.Client;
import com.oocl.chatroom.client.sender.ClientSender;
import com.oocl.chatroom.network.protocol.Message;

public class ChatUI extends JFrame {

	private Client client;
	

	Timer t = null;

	private JPanel mainPane;
	private JPanel listPanel;
	private JPanel chatPanel;
	private JPanel textPanel;

	private JButton btnSend;
	private JButton btnShake;
	private JButton btnClear;
	private JTextArea chatText;
	private JTextArea inputText;
	private JTextArea statusText;
	private JList<String> clist;
	private DefaultListModel<String> listModel;


	ClientSender send;
	private String sender;
	private String content = "";
	private int type;
	private long time;
	
	
	private JButton btnBuilder(String text,String icon){
		JButton btn=new JButton(text,new ImageIcon(icon));
		btn.setUI(new BasicButtonUI());
		//btn.setPreferredSize(new Dimension(80,27));
		btn.setContentAreaFilled(false);
		btn.setFont(new Font("Tahoma", Font.BOLD, 15));
		btn.setMargin(new Insets(0,0,0,0));
		btn.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				JButton btn=(JButton) e.getComponent();
				Border etchedBorder=new EtchedBorder(EtchedBorder.LOWERED);
				btn.setBorder(etchedBorder);
			}
			
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				//boder etched
				JButton btn=(JButton)e.getComponent();
				Border etchedBorder=new EtchedBorder(EtchedBorder.RAISED);
				btn.setBorder(etchedBorder);
			}
			
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				JButton btn=(JButton) e.getComponent();
				btn.setForeground(Color.black);//Set font color
				btn.setBorderPainted(false);//Imposed the border
			}
			
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				JButton btn=(JButton) e.getComponent();
				btn.setForeground(new Color(0x33,0x66,0xcc));//Set font color
				Border etchedBorder=new EtchedBorder(EtchedBorder.LOWERED);
				btn.setBorder(etchedBorder);
				btn.setRolloverEnabled(true);
			}
			
			public void mouseClicked(MouseEvent e) {
			}
		});
		return btn;
	}

	public ChatUI(Client client) {
		
		this.client = client;
		this.send = new ClientSender(client.getOut());
		this.sender = client.getName();
		this.setTitle(client.getName());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(673, 665);
		this.setLocationRelativeTo(null);

		mainPane = new JPanel();
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		mainPane.setLayout(null);

		listPanel = new JPanel();
		listPanel.setBounds(486, 0, 181, 637);
		listPanel.setLayout(null);

		chatPanel = new JPanel();
		chatPanel.setBounds(0, 0, 483, 494);
		chatPanel.setLayout(null);

		chatText = new JTextArea();
		chatText.setFont(new Font("Malgun Gothic", Font.PLAIN, 16));
		
		textPanel = new JPanel();
		textPanel.setBounds(0, 497, 483, 140);
		textPanel.setLayout(null);

		//btnSend = new JButton("Send");
		btnSend=btnBuilder("Send", "16.png");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				content = inputText.getText();
				if(!content.equals("")&&!clist.getSelectedValue().equals("")&&clist.getSelectedValue()!=null){
					type = 0;
					time = System.currentTimeMillis();
					Date now = new Date(time);
					int hours = now.getHours();
					int min = now.getMinutes();
					Message msg = null;
					if(clist.getSelectedValue().equals("All")){
						msg = new Message(content,type, sender,  "*", time);
					}else{
						msg = new Message(content,type, sender,clist.getSelectedValue() , time);
					}
					try {
						send.send(msg);
						inputText.setText("");
						chatText.setText(chatText.getText() + hours + ":" + min
								+ " to: "+ clist.getSelectedValue()+"\n"
								+ msg.getContent() + "\n\n");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						statusText.setText(" The Server is disconnect!");
					}
				}
				
			}
		});
		btnSend.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnSend.setBounds(384, 110, 89, 23);

		//btnShake = new JButton("Shake");
		btnShake=btnBuilder("", "05.png");
		btnShake.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!clist.getSelectedValue().equals("")){
					content = "";
					type = 1;
					time = System.currentTimeMillis();
					Message msg = null;
					Date now = new Date(time);
					int hours = now.getHours();
					int min = now.getMinutes();
					if(clist.getSelectedValue().equals("All")){
						msg = new Message( content, type,sender, "*", time);
						chatText.setText(chatText.getText() + "\t" + hours + ":" + min + " "
								+ "I shake All \n" );
					}else{
						msg = new Message( content, type,sender,clist.getSelectedValue(), time);
						chatText.setText(chatText.getText() + "\t" + hours + ":" + min + " "
								+ "I shake "+clist.getSelectedValue()+" \n" );
						
					}
					try {
						send.send(msg);
						//shake();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						statusText.setText(" The Server is shutting down!");
					}
				}
			}
		});
		btnShake.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnShake.setBounds(33, 5, 40, 23);

		inputText = new JTextArea();
		inputText.setFont(new Font("Eras Demi ITC", Font.PLAIN, 17));
		inputText.setBounds(10, 33, 463, 74);
		
		JScrollPane js=new JScrollPane(chatText);
		js.setBounds(10, 11, 463, 450);
		chatPanel.add(js);
		textPanel.add(inputText);
		textPanel.add(btnSend);
		textPanel.add(btnShake);
		mainPane.add(chatPanel);
		
		statusText = new JTextArea();
		statusText.setFont(new Font("Microsoft YaHei UI", Font.BOLD, 14));
		statusText.setForeground(SystemColor.textHighlight);
		statusText.setBounds(10, 465, 463, 22);
		chatPanel.add(statusText);
		mainPane.add(textPanel);
		
		btnClear = btnBuilder("Clear", "26.png");
		btnClear.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				inputText.setText("");
			}
		});
		btnClear.setMargin(new Insets(0, 0, 0, 0));
		btnClear.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnClear.setContentAreaFilled(false);
		btnClear.setBounds(283, 110, 89, 23);
		textPanel.add(btnClear);
		mainPane.add(listPanel);
		
		clist = new JList();
		clist.setBackground(SystemColor.menu);
		clist.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 19));
		clist.setBounds(25, 25, 150, 607);
		clist.addListSelectionListener(new ListSelectionListener(){

			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				printStatus();
			}
		});
		listPanel.add(clist);
		
		this.setContentPane(mainPane);
	}
	
	public void printStatus(){
		if(clist.getSelectedValue()==null){
			statusText.setText("");
		}
		else{
			
			statusText.setText(" To "+clist.getSelectedValue()+":");
		}
	}
	
	public void shake() {
		int x = this.getLocation().x;
		int y = this.getLocation().y;
		for (int i = 0; i < 10; i++) {
			try {
				Point endPoint = new Point(x + 10, y + 10);
				this.setLocation(endPoint);
				Thread.sleep(100);
				endPoint = new Point(x - 10, y - 10);
				setLocation(endPoint);
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void msgPrint(Message msg) {
		String txt;
		Date now = new Date(msg.getDate());
		int hours = now.getHours();
		int min = now.getMinutes();
		switch (msg.getType()) {
		case 0:
			if (msg.getReceiver().equals("*")) {
				txt = chatText.getText() + hours + ":" + min + " "
						+ msg.getSender() + " say to all:\n" + msg.getContent()
						+ "\n";
			} else {
				txt = chatText.getText() + hours + ":" + min + " "
						+ msg.getSender() + " say to me:\n" + msg.getContent()
						+ "\n";
			}
			chatText.setText(txt);
			break;
		case 1:
			if (msg.getReceiver().equals("*")) {
				txt = chatText.getText() + "\t" + hours + ":" + min + " "
						+ msg.getSender() + " shakes all.\n" + msg.getContent()
						+ "\n";
			} else {
				txt = chatText.getText() + "\t" + hours + ":" + min + " "
						+ msg.getSender() + " shakes me.\n" + msg.getContent()
						+ "\n";
			}
			chatText.setText(txt);
			shake();
			break;
		case 2:
			txt = chatText.getText() + "\t" + hours + ":" + min + " "
					+ msg.getContent() + " is online.\n";
			chatText.setText(txt);
			break;
		case 3:
			txt = chatText.getText() + "\t" + hours + ":" + min + " "
					+ msg.getContent() + " is downline.\n";
			chatText.setText(txt);
			break;
		}
	}


	
	public void printServerShutdown(Message msg){
		String txt;
		Date now = new Date(msg.getDate());
		int hours = now.getHours();
		int min = now.getMinutes();
		txt = chatText.getText() + "\t" + hours + ":" + min + " "
				+ msg.getContent() + "\n";
		chatText.setText(txt);
		
	}
	
	
	public void listRefresh(String[] name) {
		
		listPanel.removeAll();
		listModel=new DefaultListModel<String>();
		listModel.addElement("All");
		
		for (int i = 0; i < name.length; i++) {
			if(name[i]!=null&&!"".equals(name[i])){
				listModel.addElement(name[i]);
			}
		}
		clist.setModel(listModel);
		listPanel.add(clist);
		listPanel.validate();
		listPanel.repaint();
		
		}
	
	
	public void addEvent(){
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.out.println("close window");
				try {
					Message msg=new Message("",3,sender,"",System.currentTimeMillis());
					send.send(msg);
					super.windowClosing(e);;
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
	}
}
